/*
 * File    : /src/views/mark/meditor/ConfigBridge.js
 * Time    : 2021-08-24 13:00:43
 * Author  : Gray Snail
 * Version : 1.0
 * Contact : shtao2011@sina.com
 * License : Copyright (c) 2021 Gray Snail
 * Desc    : 基于字典表的配置转换为新的标注配置
 *
 * 完成新的配置管理后废除
 *
 */

import BaseConfig, { AttrType, AttrRule, LabelType, ReferDirect } from './config'

function configJosnParse(dictConfig) {
  const attrs = {}
  for (const item of dictConfig) {
    const { dictValue: key, dictLabel } = item
    let val
    // json 解析或者 基本类型解析
    try {
      val = Number(dictLabel)
      if (!isNaN(val)) {
        // nothing
      } else if (dictLabel === 'true' || dictLabel === 'false') {
        val = dictLabel === 'true'
      } else {
        val = JSON.parse(dictLabel)
      }
    } catch (err) {
      console.warn('[Config bridge]', err)
      val = dictLabel
    }
    attrs[key] = val
  }
  return attrs
}

function labelAttrInit(attributeNames, prefix = '') {
  const attrs = {}
  // 2d,3d
  for (let j = 2; j <= 3; j++) {
    // a{i}_2d, a{i}_3d, a{i}_2d_description, a{i}_3d_description
    for (let i = 1; i <= 8; i++) {
      const field = 'a' + i + '_' + j + 'd'
      const key = prefix + field

      if (key in attributeNames) {
        const { typeName: name, typeDesc: desc, values } = attributeNames[key]
        attrs[field] = { name, desc, input: AttrType.SELECT, field, values }
      }
    }

    // b{i}_2d, b{i}_3d, b{i}_2d_description, b{i}_3d_description
    for (let i = 1; i <= 8; i++) {
      const field = 'a' + i + '_' + j + 'd'
      const key = prefix + field
      if (key in attributeNames) {
        const { typeName: name, typeDesc: desc, values } = attributeNames[key]
        attrs[field] = { name, desc, input: AttrType.SELECT, field, values }
      }
    }
  }
  return attrs
}

function buildView2D(attrs) {
  // view2D
  const view2D = this.view2D

  if ('size_2d' in attrs) view2D.size_2d = attrs['size_2d']

  const { showTip = 'show', g_point, zoomStricked } = attrs
  view2D.showTip = showTip !== 'hide'
  view2D.g_point = g_point || true
  view2D.zoomStricked = zoomStricked || true

  const { pointStrokeWidth = 2, lineStrokeWidth = 2, polyStrokeWidth = 2, rectStrokeWidth = 2 } = attrs
  view2D.stroke = { point: pointStrokeWidth, line: lineStrokeWidth, poly: polyStrokeWidth, rect: rectStrokeWidth }
}

function buildView3D(attrs) {
  // view3D
  const view3D = this.view3D
  view3D.renderType = attrs['renderType']
  view3D.cameraPosition = attrs['cameraPosition']
  view3D.highLightCubePoints = attrs['highLightCubePoints']
  view3D.size_3d = attrs['show3dSize']
}

function buildReferLines(attrs) {
  // refer lines
  const referLines = this.referLines
  const splitLine = attrs['splitLine2d'] || attrs['splitLine']
  if (splitLine instanceof Number) {
    const line = { direct: ReferDirect.VERTICAL, val: splitLine, width: 1, color: 'red' }
    referLines.push(line)
  } else if (splitLine instanceof Array) {
    for (const lineConf of splitLine) {
      const line = {
        direct: ReferDirect.HORIZON,
        val: Number(lineConf['value']),
        width: 1,
        color: lineConf['color'],
        desc: lineConf['description'],
      }
      referLines.push(line)
    }
  } else {
    console.warn('[ConfigBridge] : splitLine2d config error')
  }
  // this.referLines = referLines
}

function buildToolConfig(attrs) {
  const {
    toolbox,
    labels: { values },
  } = this

  values.forEach((label) => {
    switch (label.type) {
      case LabelType.RECT_2D:
        toolbox.rect = toolbox.rect || {}
        break
      case LableType.RECT_PT_2D:
        toolbox.rectPoint1 = toolbox.rectPoint1 || {}
        break
      case LableType.SEGMENT_2D:
        break
      case LableType.POLY_2D:
      case LableType.LINE_2D:
        toolbox.line = toolbox.line || {}
        break
      case LableType.RECT_3D:
        toolbox.cube = toolbox.cube || {}
        break
    }
  })

  const gridConfig = attrs['gridConfig']
  if (gridConfig) {
    const { rowCount = 2, columnCount = 0 } = gridConfig
    toolbox.grid = { rowCount, columnCount }
  }
  // mock
  toolbox.line = {}
  toolbox.poly = {}
  toolbox.rectPoint1 = {}
}

export default class ConfigBridge extends BaseConfig {
  // config = DefaultConfig

  constructor(task, dictConfig, dictColorConfig) {
    super()

    if (!task) {
      console.error('[Config bridge]: Task is not empty.')
    } else {
      const markType = Number(task.markType)
      this.track = markType !== 10
      if (markType > 0 && markType < 9) {
        this.imageCount = markType
        this.pcdCount = 1
      } else if (markType === 0) {
        this.imageCount = 0
        this.pcdCount = 1
      } else if (markType === 9) {
        this.imageCount = 0
        this.pcdCount = 1
      } else if (markType === 10) {
        this.imageCount = 1
        this.pcdCount = 0
      }
    }

    const attrs = configJosnParse(dictConfig)

    // common
    const { common } = this
    common.fusionView = attrs['radarView']

    const colorMap = {}
    for (const item of dictColorConfig) {
      const { dictValue: key, dictLabel } = item
      colorMap[key] = dictLabel
    }

    const commonAttrs = labelAttrInit(attrs)
    this.attrs = commonAttrs

    // 附加属性处理
    const labels = this.labels
    labels.values = []
    for (const name in attrs.labelName.values) {
      const desc = attrs.labelName.values[name]
      const myAttrs = Object.assign({}, commonAttrs, labelAttrInit(attrs, name + '_'))
      labels.values.push({ name, desc, color: colorMap[name], type: LabelType.RECT_2D, attrs: myAttrs })
    }

    buildView2D.call(this, attrs)
    buildReferLines.call(this, attrs)
    buildView3D.call(this, attrs)

    buildToolConfig.call(this, attrs)

    console.log('[Config bridge]: Build config finished.')
    // console.log(attrs)
  }

  // 获取标注的名称
  label(annotation) {
    console.log('[Config bridge]: Not implement')
  }
}
